﻿using Microsoft.Win32;
using Prism.Mvvm;
using UC = Wpf.Ui.Controls;

namespace K9Tools.UI;

public static class GlobalCommands
{
    #region 文件夹
    private static DelegateCommand<UC.TextBox>? _chooseFolder;
    public static DelegateCommand<UC.TextBox>? ChooseFolder =>
        _chooseFolder ??= new DelegateCommand<UC.TextBox>(textBox =>
    {
        if (textBox is null)
        {
            return;
        }
        var dialog = new OpenFolderDialog
        {
            Multiselect = false,
            Title = "选择文件夹"
        };
        if (dialog.ShowDialog() == true)
        {
            textBox.Text = dialog.FolderName;
        }
    });
    #endregion

    #region 文件

    private static DelegateCommand<UC.TextBox>? _chooseFile;
    public static DelegateCommand<UC.TextBox>? ChooseFile =>
        _chooseFile ??= new DelegateCommand<UC.TextBox>(textBox =>
        {
            if (textBox is null)
            {
                return;
            }
            var dialog = new OpenFileDialog
            {
                Multiselect = false,
                Title = "选择文件",
                AddExtension = true,
                CheckFileExists = false,
                DefaultExt = "*.docx",
            };
            if (dialog.ShowDialog() == true)
            {
                textBox.Text = dialog.FileName;
            }
        });


    private static DelegateCommand<UC.TextBox>? _chooseNewFile;

    public static DelegateCommand<UC.TextBox>? ChooseNewFile =>
        _chooseNewFile ??= new DelegateCommand<UC.TextBox>(textBox =>
        {
            if (textBox is null)
            {
                return;
            }
            var dialog = new SaveFileDialog
            {
                Title = "选择新文件",
                AddExtension = true,
                DefaultExt = "*.docx",
                Filter = "Word 文档 (*.docx)|*.docx|所有文件 (*.*)|*.*"
            };
            if (dialog.ShowDialog() == true)
            {
                textBox.Text = dialog.FileName;
            }
        });

    #endregion
}
